package com.fm.service;

import lombok.extern.slf4j.Slf4j;

import java.io.*;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.UUID;

import static com.fm.Utils.utils.*;

@Slf4j
public class service_upload {


    /**
     * 上传
     *
     * @param socket
     */
    public static void UpLoadFile(Socket socket) {
        try {

            //获取名字
            String fileName;
            byte[] bytes = new byte[1024];
            DataInputStream dataInputStream = new DataInputStream(socket.getInputStream());
            int len = dataInputStream.readInt();
            socket.getInputStream().read(bytes, 0, len);
            fileName = new String(bytes, 0, len);
//            log.info("文件名:  "+s);


            //创建文件对象
            UUID uuid = UUID.randomUUID();              //uuid
            String key = "uploadFileName." + uuid;
            writePropertyData(key, fileName);               //映射
            FileOutputStream fileOutputStream = new FileOutputStream("src/main/resources/upLoad/" + uuid);

            //获取数据
            while ((len = (socket.getInputStream().read(bytes))) != -1) {
                fileOutputStream.write(bytes, 0, len);
            }

            //接收完成给反馈
            socket.getOutputStream().write("上传成功".getBytes());
            socket.getOutputStream().close();

            //关闭
            fileOutputStream.close();
            socket.close();

        } catch (IOException e) {
            throw new RuntimeException(e);
        }


    }

    //
//    public void UDPServer(){
//        int port = 12345;
//
//        try (DatagramSocket socket = new DatagramSocket(port)) {
//            log.info("服务运行在端口:  " + port);
//
//            while (true) {
//                // 接收文件名
//                byte[] fileNameBuffer = new byte[1024];
//                DatagramPacket fileNamePacket = new DatagramPacket(fileNameBuffer, fileNameBuffer.length);
//                socket.receive(fileNamePacket);
//
//                // 获取文件名
//                String fileName = new String(fileNamePacket.getData(), 0, fileNamePacket.getLength());
//                log.info("接收的文件名: " + fileName);
//
//                // 创建并启动处理线程
//                Thread uploadThread = new UploadThread(socket, fileName, fileNamePacket.getAddress(), fileNamePacket.getPort());
//                uploadThread.start();
//            }
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//    }
//
//    static class UploadThread extends Thread {
//        private DatagramSocket socket;
//        private String fileName;
//        private byte[] fileData;
//        private String clientAddress;
//        private int clientPort;
//
//        public UploadThread(DatagramSocket socket, String fileName, InetAddress clientAddress, int clientPort) {
//            this.socket = socket;
//            this.fileName = fileName;
//            this.clientAddress = String.valueOf(clientAddress);
//            this.clientPort = clientPort;
//        }
//
//        @Override
//        public void run() {
//            try {
//                FileOutputStream fileOutputStream = new FileOutputStream("src/main/resources/upLoad/" + fileName);
//
//                while (true) {
//                    byte[] buffer = new byte[1024];
//                    DatagramPacket dataPacket = new DatagramPacket(buffer, buffer.length);
//                    socket.receive(dataPacket);
//                    int bytesRead = dataPacket.getLength();
//
//                    if (bytesRead <= 0|| new String(buffer,0, buffer.length).equals("quit")){
//                        log.info("文件名接收结束");
//                        break; // 文件接收结束
//                    }
//
//                    // 写入文件
//                    fileOutputStream.write(dataPacket.getData(), 0, bytesRead);
//                }
//
//                // 发送上传成功的消息给客户端
//                byte[] successMessage = "上传成功".getBytes();
//                DatagramPacket successPacket = new DatagramPacket(successMessage, successMessage.length,
//                        InetAddress.getByName(clientAddress), clientPort);
//                socket.send(successPacket);
//
//                // 关闭文件流
//                fileOutputStream.close();
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//        }
//    }


}
